package com.mk.cloud.common.service.core;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.config.AppConfig;
import com.mk.cloud.common.domain.CpMsg;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import com.mk.cloud.common.util.DateUtils;
import org.osgl.util.S;import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;

@Controller("cpMsg")
public class CpMsgService extends CookieService {

    @Inject
    private CpMsg.Mapper cpMsgMapper;

    @Description("新增消息")
    @PostAction("add.json")
    public Integer add(CpMsg msg){
        cpMsgMapper.insert(msg);
        return 0;
    }

    /**
     * 增加并发送推送消息消息
     * 不能使用me中的信息
     * @param mtitle
     * @param mmsg
     */
    @With({ICLogin.class,ITransacte.class})
    public void addAndSend(String mtitle,String mmsg,String userid,String coid){
        CpMsg msg = new CpMsg();
        msg.setMid(S2.UUID());

        msg.setMtitle(mtitle);
        msg.setMsg(mmsg);

        msg.setMurl(AppConfig.msg.path.get());
        msg.setMtime(DateUtils.getTimeNow());
        msg.setForshow(GlobalValues.FLAG_YES.value());
        msg.setReadabled(GlobalValues.FLAG_NO.value());
        msg.setUserid(userid);
        msg.setMstate(GlobalValues.FLAG_NO.value());//TODO 该字段数据库没有
        msg.setMsgtype(GlobalValues.MSTYPE_0.value());
        msg.setCoid(coid);
        this.add(msg);
        WsService.handleNewsUpdate(msg);
    }
    @With({ICLogin.class,ITransacte.class})
    public void addAndSend(String mtitle,String mmsg,String name,String key,String userid,String coid){
        CpMsg msg = new CpMsg();
        msg.setMid(S2.UUID());
        msg.setAttachmentname(name);
        msg.setAttachmenturl(key);
        msg.setMtitle(mtitle);
        msg.setMsg(mmsg);

        msg.setMurl(AppConfig.msg.path.get());
        msg.setMtime(DateUtils.getTimeNow());
        msg.setForshow(GlobalValues.FLAG_YES.value());
        msg.setReadabled(GlobalValues.FLAG_NO.value());
        msg.setUserid(userid);
        msg.setMstate(GlobalValues.FLAG_NO.value());//TODO 该字段数据库没有
        msg.setMsgtype(GlobalValues.MSTYPE_0.value());
        msg.setCoid(coid);
        this.add(msg);
        WsService.handleNewsUpdate(msg);
    }

    private void readMsg(String mid){
        CpMsg msg = new CpMsg();
        msg.setMid(mid);
        msg.setReadabled(GlobalValues.FLAG_YES.value());
        msg.setReadtime(DateUtils.getTimeNow());
        cpMsgMapper.updateTemplateById(msg);
    }


    @Description("阅读消息")
    @GetAction("read.json")
    @With({ICLogin.class,ITransacte.class})
    public PageQuery<CpMsg> read(String mid){
        readMsg(mid);
        return myCpMsgs(1,5);
    }

    @Description("查看消息")
    @GetAction("view.json")
    @With({ICLogin.class,ITransacte.class})
    public CpMsg view(String mid){
        readMsg(mid);
        return cpMsgMapper.viewMyMsg(me.getUserid(),mid);
    }

    @Description("删除消息")
    @DeleteAction("delete.json")
    @With({ICLogin.class,ITransacte.class})
    public Integer delete(String mid){
        CpMsg msg = new CpMsg();
        msg.setMid(mid);
        msg.setReadabled(GlobalValues.FLAG_YES.value());
        msg.setReadtime(DateUtils.getTimeNow());
        msg.setForshow(GlobalValues.FLAG_NO.value());
        cpMsgMapper.updateTemplateById(msg);
        return 0;
    }




    @GetAction("mymsgs.json")
    @Description("查询我的消息列表")
    @With({ICLogin.class, ITransacte.class})
    public PageQuery<CpMsg> myCpMsgs(
            @Description("当前第几页") @DefaultValue("1") Integer currentPage,
            @Description("每页多少条") @DefaultValue("5") Integer pageSize){
        PageQuery<CpMsg> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String,String> map = new HashMap<>();

        map.put("userid",me.getUserid());
        query.setParas(map);
        cpMsgMapper.getSQLManager().pageQuery("cpMsg.select_my_msg_list",CpMsg.class,query);
        return query;
    }

    @GetAction("myCpMsgPageList.json")
    @Description("我的消息列表")
    @With({ICLogin.class, ITransacte.class})
    public PageQuery<CpMsg> myCpMsgPageList(
            @Description("筛选条件:消息内容") String msg,
            @Description("筛选条件:开始时间") String bdate,
            @Description("筛选条件:结束时间") String edate,
            @Description("当前第几页") @DefaultValue("1") Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize){
        PageQuery<CpMsg> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String,String> map = new HashMap<>();
        map.put("userid",me.getUserid());
        map.put("msg",msg);
        map.put("bdate",bdate);
        map.put("edate",edate);
        query.setParas(map);
        cpMsgMapper.getSQLManager().pageQuery("cpMsg.select_all_my_msg_list",CpMsg.class,query);
        return query;
    }

}
